package com.rxy.zkclient;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

public class Get_NodeChildren {
    public static void main(String[] args) throws InterruptedException {
        ZkClient zkClient = new ZkClient("192.168.31.247:2181");
        // 获取子节点列表
        zkClient.createPersistent("/rxy-zkClient/c1");
        List<String> children = zkClient.getChildren("/rxy-zkClient");
        System.out.println(children);
        // 注册监听事件
        /**
         * 客户端可以对一个不存在的节点进行子节点变更的监听，
         * 只要该节点的子节点列表或者该节点本身被创建或者删除，都会触发监听
         */
        zkClient.subscribeChildChanges("/rxy-zkClient-get", new IZkChildListener() {
            @Override
            public void handleChildChange(String parentPath, List<String> list) throws Exception {
                System.out.println(parentPath + "的子节点列表发生了变化，变化后的子节点列表为" + list);
            }
        });
        // 测试
        zkClient.createPersistent("/rxy-zkClient-get");
        Thread.sleep(1000);
        zkClient.createPersistent("/rxy-zkClient-get/c1");
        Thread.sleep(1000);
        zkClient.deleteRecursive("/rxy-zkClient-get");
        Thread.sleep(1000);
    }
}
